package com.thread.interruptThread;

/**
 * 方法 2：抛出异常法
 */
public class InterruptThread2 implements Runnable {

	private double d;

	public void run() {
		//死循环执行打印"I am running!" 和做消耗时间的浮点计算
		try {
			while (true) {
				System.out.println("I am running!");
				for (int i = 0; i < 900000; i++) {
					d = d + (Math.PI + Math.E) / d;
				}
				//休眠一断时间,中断时会抛出InterruptedException
				Thread.sleep(50);
			}
		} catch (InterruptedException e) {
			System.out.println("ATask.run() interrupted!");
		}
	}


	public static void main(String[] args) throws InterruptedException {
		InterruptThread2 target = new InterruptThread2();
		Thread t = new Thread(target);
		t.start();

		Thread.sleep(100);

		System.out.println("开始中断线程。。。。。。");

		t.interrupt();
	}
}
